package com.moetube.video.controller;


import com.github.pagehelper.Page;
import com.moetube.entity.PageResult;
import com.moetube.entity.Result;
import com.moetube.entity.StatusCode;
import com.moetube.video.pojo.Barrage;
import com.moetube.video.pojo.Comment;
import com.moetube.video.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {



    @Autowired
    private CommentService commentService;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Comment> CommentList = commentService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",CommentList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id){
        Comment Comment = commentService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",Comment);
    }


    /***
     * 新增数据
     * @param Comment
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Comment Comment){
        commentService.add(Comment);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param Comment
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Comment Comment,@PathVariable Long id){
        Comment.setId(id);
        commentService.update(Comment);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        commentService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Comment> list = commentService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Comment> pageList = commentService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


    @GetMapping("/video/{size}/{page}/{videoId}")
    public Result<List<Comment>> findListByVideoId(@PathVariable  int page, @PathVariable  int size,@PathVariable("videoId") Long videoId) {
        List<Comment> commentList = commentService.findList(page, size, videoId);
        return new Result(true,StatusCode.OK,"查询成功",commentList);
    }
//    /**
//     * 根据商品分类名称查询品牌列表
//     * @param category
//     * @return
//     */
//    @GetMapping("/category/{category}")
//    static Result<List<Map>> findListByCategoryName(@PathVariable String category){
//        List<Map> CommentList = commentService.findListByCategoryName(category);
//        return new Result(true,StatusCode.OK,"查询成功",CommentList);
//    }
    @GetMapping("/video/{videoId}")
    public Result<List<Comment>> findListByVideoId(@PathVariable("videoId") String videoId) {

        List<Comment> commentList = commentService.findList(videoId);
        for (Comment comment : commentList) {
            int num = new Random().nextInt(2);
            if(0==num){
                comment.setUserId("是德国是德国");
            }
            if(1==num){
                comment.setUserId("捏给你地方");
            }
            if(2==num){
                comment.setUserId("四个不是");
            }
        }

        return new Result<>(true,StatusCode.OK,"查询成功",commentList);
    }

    /**
     * 查询视频评论总数
     * @param videoId
     * @return
     */
    @GetMapping("/findCommentTotal/{videoId}")
    public Result findCommentTotal(@PathVariable("videoId") Long videoId){
        int commentTotal = commentService.findCommmentById(videoId);
        return new Result(true,StatusCode.OK,"查询成功",commentTotal);
    }



}
